﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using SmokinAces.Object_Classes.Objects;
namespace SmokinAces.Object_Classes
{
    class Map
    {
        static private Color[] FlowerColors = {
                                            new Color(255, 0,0),
                                            new Color(0, 0,255),
                                            new Color(255, 255,0),
                                            new Color(255, 0,255),
                                            new Color(255, 125,0)
                                           };
        public static void Create_Map(GraphicsDevice device, Game_Engine parent)
        {
            const int divisor = 100;
            int viewHeight = device.Viewport.Height;
            int viewWidth = device.Viewport.Width;
            Random rand = new Random();
            List<Game_Object> MapTiles = new List<Game_Object>();
            int numTilesY = 64; // viewHeight / divisor; //so we have exactly 32 tiles across
            int numTilesX = 64; // viewWidth / divisor; //so we have exactly 32 tiles down
            int[] colorvertices = new int[1088];
            for (int i = 0; i < 1088; i+=2)
            {
                colorvertices[i] = rand.Next(0, Map_Colors.GrassColorList.Length);
                colorvertices[i+1] = colorvertices[i];
            }

            int prevI = 0;
            int prevJ = 0;

            for (int j = 0; j <= divisor; j ++)
            {
                for (int i = 0; i <= divisor; i ++)
                {
                    int[] CornerColors = new int[4];
                    CornerColors[0] = colorvertices[Math.Abs(prevI + (prevJ * divisor)) % 1088]; //UL
                    CornerColors[1] = colorvertices[Math.Abs(i + (prevJ * divisor)) % 1088];//UR
                    CornerColors[2] = colorvertices[Math.Abs(prevI + (j * divisor)) % 1088];//BL
                    CornerColors[3] = colorvertices[Math.Abs(i + (j * divisor)) % 1088];//BR
                    Map_Tile tempTile = new Map_Tile(prevI * numTilesX, prevJ * numTilesY, Weather_Type.None, Enviornment_Type.Normal, Tile_Type.Grass, CornerColors, parent);
                    MapTiles.Add(tempTile);
                    prevI = i;
                }
                prevI = 0;
                prevJ = j;
            }
            List<int> poslist = new List<int>();
            for (int i = 0; i < 900;  i++)
            {
                int left;
                int top;
                int temp;
                do
                {
                    left = rand.Next(-(64 / 16), 6400 / 16);
                    top = rand.Next(-(64 / 16), 6400 / 16);
                    temp = (left + (top * ((6400 / 16) + 1)));
                } while (poslist.Contains(temp));
                poslist.Add(temp);
                left *= 16;
                top *= 16;
                Flower tempFlower = new Flower(left, top, FlowerColors[rand.Next(0, FlowerColors.Length)],rand.Next(1, 10), parent);
                MapTiles.Add(tempFlower);
            }
            poslist.Clear();
            for (int i = 0; i < 400; i++)
            {
                int left;
                int top;
                int temp;
                do
                {
                    left = rand.Next(-((64 / 64) - 1), (6400 / 128) - 1) * 128;
                    top = rand.Next(-((64 / 64) - 1), (6400 / 64) - 1) * 64;
                    temp = (left + (top * ((6400 / 64) + 1)));
                }
                while(poslist.Contains(temp));
                poslist.Add(temp);
                int enumindex = rand.Next(0, 2);
                Bushes tempBush = new Bushes(left, top, (Bush_Type)Enum.GetValues(typeof(Bush_Type)).GetValue(enumindex), parent);
                MapTiles.Add(tempBush);
            }
            poslist.Clear();
            for (int i = 0; i < 200; i++)
            {
                int left;
                int top;
                int temp;
                do
                {
                    left = rand.Next(-((32 / 32) - 1), (6400 / 32) - 1) * 32;
                    top = rand.Next(-((32 / 32) - 1), (6400 / 32) - 1) * 32;
                    temp = (left + (top * ((6400 / 32) + 1)));
                }
                while (poslist.Contains(temp));
                poslist.Add(temp);
                //int enumindex = rand.Next(0, 3);
                Pot tempPot = new Pot(left, top, parent);
                MapTiles.Add(tempPot);
            }
            poslist.Clear();
            for (int i = 0; i < 400; i++)
            {
                int left;
                int top;
                int temp;
                do
                {
                    left = rand.Next(-((128 / 128) - 1), (12800 / 128) - 1) * 64;
                    top = rand.Next(-((64 / 64) - 1), (6400 / 64) - 1) * 64;
                    temp = (left + (top * ((6400 / 64) + 1)));
                }
                while (poslist.Contains(temp));
                poslist.Add(temp);
                int enumindex = rand.Next(0, 3);
                Tree tempTree = new Tree(left, top, (Tree_Type)Enum.GetValues(typeof(Tree_Type)).GetValue(enumindex), parent);
                MapTiles.Add(tempTree);
            }
            poslist.Clear();
            for (int i = 0; i < 100; i++)
            {
                int left;
                int top;
                int temp;
                do
                {
                    left = rand.Next(-((64 / 64) - 1), (6400 / 64) - 1) * 64;
                    top = rand.Next(-((64 / 64) - 1), (6400 / 64) - 1) * 64;
                    temp = (left + (top * ((6400 / 64) + 1)));
                }
                while (poslist.Contains(temp));
                poslist.Add(temp);
                int enumindex = rand.Next(0, 3);
                NPC tempNpc = new NPC(left, top, (NPC_Type)Enum.GetValues(typeof(NPC_Type)).GetValue(enumindex), parent);
                MapTiles.Add(tempNpc);
            }

            parent.Hero = new Main_Character(new Vector3(3200.0f + 512.0f, 3200.0f + 384.0f, 0.0f), parent);
            parent.NewMap(MapTiles,100 * 64,100 * 64);
        }
    }
}

